package com.yescall.mole.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.OnActionCompleted;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;
import com.badlogic.gdx.scenes.scene2d.actions.FadeOut;
import com.badlogic.gdx.scenes.scene2d.actions.Sequence;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Scaling;
import com.yescall.mole.MoleGame;

public class SplashScreen extends AbstractScreen {
	
	private Texture splashTexture;
    private TextureRegion splashRegion;

	public SplashScreen(MoleGame game) {
		super(game);
		// TODO Auto-generated constructor stub
	}
	
	public void show(){
        super.show();
 
        // load the splash image and create the texture region
        splashTexture = new Texture( "data/libgdx.png" );
 
        // we set the linear texture filter to improve the stretching
        splashTexture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
 
        // in the image atlas, our splash image begins at (0,0) at the
        // upper-left corner and has a dimension of 512x301
        //splashTextureRegion = new TextureRegion( splashTexture, 0, 0, 512, 301 );
        
        // in the image atlas, our splash image begins at (0,0) of the
        // upper-left corner and has a dimension of 512x301
        splashRegion = new TextureRegion( splashTexture, 0, 0, 512, 301 );

        // here we create the splash image actor and set its size
        Image splashImage = new Image( splashRegion, Scaling.stretch, Align.BOTTOM | Align.LEFT );
        splashImage.setFillParent( true );
        
        // this is needed for the fade-in effect to work correctly; we're just
        // making the image completely transparent
        splashImage.color.a = 0f;

        Sequence actions = Sequence.$( FadeIn.$( 0.75f ), Delay.$( FadeOut.$( 0.75f ), 1.75f ) );
        actions.setCompletionListener( new OnActionCompleted() {
         public void completed( Action action ) {
          game.setScreen( new MainScreen( game ) );
         }
        } );
        splashImage.action( actions );

        // and finally we add the actors to the stage, on the correct order
        stage.addActor( splashImage );
    }
	
	@Override
    public void resize(int width,int height )
    {
        super.resize( width, height );

        // let's make sure the stage is clear
        stage.clear();

        // in the image atlas, our splash image begins at (0,0) of the
        // upper-left corner and has a dimension of 512x301
        TextureRegion splashRegion = new TextureRegion( splashTexture, 0, 0, 512, 301 );

        // here we create the splash image actor and set its size
        Image splashImage = new Image( splashRegion, Scaling.stretch, Align.BOTTOM | Align.LEFT );
        splashImage.width = width;
        splashImage.height = height;

        // this is needed for the fade-in effect to work correctly; we're just
        // making the image completely transparent
        splashImage.color.a = 0f;

        Sequence actions = Sequence.$( FadeIn.$( 0.75f ), Delay.$( FadeOut.$( 0.75f ), 1.75f ) );
        actions.setCompletionListener( new OnActionCompleted() {
         public void completed( Action action ) {
          game.setScreen( new MainScreen( game ) );
         }
        } );
        splashImage.action( actions );

        // and finally we add the actors to the stage, on the correct order
        stage.addActor( splashImage );
    }
 
    @Override
    public void render(float delta )
    {
        super.render( delta );
 
        // we use the SpriteBatch to draw 2D textures (it is defined in our base
        // class: AbstractScreen)
        batch.begin();
 
        // we tell the batch to draw the region starting at (0,0) of the
        // lower-left corner with the size of the screen
        batch.draw( splashRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
 
        // the end method does the drawing
        batch.end();
    }
 
    @Override
    public void dispose()
    {
        super.dispose();
        splashTexture.dispose();
    }
}
